package com.crayfish.materiallearn;

import android.app.ActivityOptions;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.transition.Explode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.crayfish.materiallearn.controllersample.GameViewActivity;
import com.crayfish.materiallearn.interactivechart.MainToActivity;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private String[] mDataset;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        recyclerView = (RecyclerView)super.findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(mLayoutManager);
//        recyclerView.addItemDecoration(new MyItemDecoration(this,MyItemDecoration.VERTICAL_LIST));
        mAdapter = new MyAdapter(mDataset);
        recyclerView.setAdapter(mAdapter);
    }

    private void initData(){
        mDataset = new String[]{
                "材料主题",
                "elevation高度和Outline",
                "列表与卡片",
                "使用图片",
                "定制动画",
                "drawer lyout",
                "Gestures",
                "Tracking",
                "InteractiveLineGraphView",
                "game_controller_input"
        };
    }

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{

        private String[] mDataset;

        public class ViewHolder extends RecyclerView.ViewHolder{
            public TextView textView;
            public CardView cardView;
            public ImageView imageView;
            public ViewHolder(View itemView) {
                super(itemView);
                textView = (TextView)itemView.findViewById(android.R.id.text1);
                cardView = (CardView)itemView.findViewById(R.id.card_view);
                imageView = (ImageView)itemView.findViewById(R.id.image);
            }
        }

        public MyAdapter(String[] mDataset) {
            this.mDataset = mDataset;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.cardview_layout,parent,false);
            ViewHolder vh = new ViewHolder(view);
            return vh;
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
            holder.textView.setText(mDataset[position]);
            holder.cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                switch (position){
                    case 0:
                        myStartActivity(MaterialThemeActivity.class);
                        break;
                    case 1:
                        myStartActivity(ElevationActivity.class);
                        break;
                    case 2:
                        Toast.makeText(MainActivity.this, "请看当前列表", Toast.LENGTH_SHORT).show();
                        break;
                    case 3:
                        myStartActivity(UseImageActivity.class);
                        break;
                    case 4:
                        Intent intent = new Intent(MainActivity.this,AnimatorActivity.class);
                        startActivity(intent, ActivityOptions
                                .makeSceneTransitionAnimation(MainActivity.this,holder.imageView,"img").toBundle());
                        break;
                    case 5:
                        myStartActivity(DrawerLayoutActivity.class);
                        break;
                    case 6:
                        myStartActivity(GesturesActivity.class);
                        break;
                    case 7:
                        myStartActivity(TrackingActivity.class);
                        break;
                    case 8:
                        myStartActivity(MainToActivity.class);
                        break;
                    case 9:
                        myStartActivity(GameViewActivity.class);
                        break;
                }
                }
            });
        }

        @Override
        public int getItemCount() {
            return mDataset.length;
        }
    }

    private void myStartActivity(Class clazz){
        getWindow().setExitTransition(new Explode());
        Intent intent = new Intent(MainActivity.this,clazz);
        startActivity(intent, ActivityOptions
                .makeSceneTransitionAnimation(MainActivity.this).toBundle());
    }

}
